Ext.onReady(function(){
	//初始化Extjs   
    Ext.QuickTips.init();   
    Ext.BLANK_IMAGE_URL ='../scripts/ext/resources/images/default/s.gif';  
    
    var addWinForm;
    var addDictWin;
    
    //====================================================================================//
	//加载查询框的数据selStore
	var selStore = new Ext.data.JsonStore({
		url:'dict_queryDictFlag',
		root:'jsonToResult',
		//autoLoad:true,
		//fields:[{name:'dictId'},{name:'dictValue'},{name:'dictName'},{name:'dictFlag'},{name:'chrnote'}]
		fields:['dict_Flag']
	});
    selStore.load();
    //条件查询Form
    var queryForm = new Ext.form.FormPanel({
		labelAlign:'right',
		buttonAlign:'center',
		frame:true,
    	items:[
    		{
				fieldLabel:'字典标志',
				xtype:'combo',
				itemCls :'float_left',
				store:selStore,
        		triggerAction: 'all',
        		selectOnFocus:true,
				valueField :'dict_Flag',
				displayField :'dict_Flag',
				editable:false,
				emptyText:'请选择一个标志....',
				id:'comboFlag'
			},
			{
				xtype :'button',
				text:'查询',
				pressed : false,
				width:50,
				handler:function(){
					var param = Ext.getCmp("comboFlag").value;
					if(param == null){
						alert('请先选择一个标志!');
						return false;
					}else{
						store.reload({params:{dictFlagSel:param}});
					}
				}
			},
			{
				xtype :'button',
				text:'查询全部',
				pressed : false,
				width:50,
				handler:function(){
					store.reload({params:{dictFlagSel:''}});
				}
			}
    	],
    	height:80,
    	//width:500,
		title:'查询条件',
    	renderTo:'queryCondition'
    });
    
    
    
    
    
    //====================================================================================//
	//Grid的Store
    var store = new Ext.data.JsonStore({
		url:'dict_queryDict',
		totalProperty:'totalPage',
		root:'resultModel',
		idProperty:'dictId',
		//fields:[{name:'dictId'},{name:'dictValue'},{name:'dictName'},{name:'dictFlag'},{name:'chrnote'}]
		fields:['dictId','dictValue','dictName','dictFlag','chrnote']
	});
	store.load({params:{start:0,limit:5}});
	
	var rowNumber = new Ext.grid.RowNumberer({header:'序号',width:30,dataIndex:'dictId'});	//显示序号
	var sm = new Ext.grid.CheckboxSelectionModel({widht:30});//显示选择框
 	//编辑器
 	var editor = new Ext.ux.grid.RowEditor({
        saveText: '更新',
        cancelText:'取消'
    });
	//Grid的ColModel
	var cm = new Ext.grid.ColumnModel([
		rowNumber,
		//sm,
	    { header: "字典值", width:100,  sortable: true,dataIndex:'dictValue',editor: {
                xtype: 'numberfield',
                allowBlank: false,
                emptyText:'请输入数字..'
            }},
	    { header: "字典名称", width:100, sortable: true,dataIndex:'dictName',editor: {
                xtype: 'textfield',
                allowBlank: false,
                emptyText:'请输入名称..'
            }},
	    { header: "字典标志", width:100, sortable: true,dataIndex:'dictFlag',editor: {
                xtype: 'textfield',
                allowBlank: false,
                emptyText:'请输入标志..'
            }},
	    { header: "备注", width:100, sortable: true,dataIndex:'chrnote',editor: {
                xtype: 'textfield',
                emptyText:'请输入备注..'
            }}
 	]);
 	
	//Grid
	var grid = new Ext.grid.GridPanel({
		store:store,
		plugins: [editor],
		cm:cm,
		//sm:sm,
		margins:'5 5 5 5',
		buttonAlign :'center',
		buttons:[
			{
				type:'button',
				text:'新增',
				handler:function(){
					editor.stopEditing();		//停止编辑
					addDictWin.show();
				}
			},
			{
				type:'button',
				text:'删除',
				handler:function(){
					editor.stopEditing();		//停止编辑
					//var count = grid.getSelectionModel().getCount();	//返回选取的总行数
					var sels = grid.getSelectionModel().getSelections();	//返回所选的纪录
					if(sels.length > 0){
						Ext.Msg.confirm('警告','确定要删除所选的行?',function(buttons){
							if(buttons == "yes"){
								var dictIDS = new Array();
								dictIDS.push(sels[0].data.dictId);
								Ext.Ajax.request({
									url:'dict_deleteDict',
									method:'POST',
									params : {'dict.dictId' : dictIDS},
									success:function(response,options){
										Ext.Msg.alert('提示','删除成功!');
										store.reload();
										selStore.reload();
									},
									failure:function(response,options){
										Ext.Msg.alert('提示','提交失败!');
									}
								});
							}else{
								return false;
							}
						});
					}else{
						Ext.Msg.alert('提示','请选择要删除的行');
						return false;
					}
				}
			}
		],
		bbar:new Ext.PagingToolbar({
			store:store,
			pageSize:5,
			emptyMsg:'没有找到相应的记录',
			displayMsg:'总记录数 {0} - {1} of {2}',
			style: "padding:0 20px 0 0",  
			autoWidth:true,
			displayInfo:true
		}),
		title:'字典管理',
		height:300,
		//width:500,
		autoWidth:true,
		renderTo:'queryResult'
	});
	//当用户提交更新时执行的操作
	editor.on(
	{
		afteredit:function(roweditor,object,record,number){		//执行的方法传入的参数 !
			Ext.Ajax.request({
				url:'dict_addDict',
				method:'POST',
				params : {'dict.dictId':record.data.dictId,'dict.dictValue':record.data.dictValue,'dict.dictName':record.data.dictName,
				'dict.dictFlag':record.data.dictFlag,'dict.chrnote':record.data.chrnote},
				waitTitle:'请稍等....',
				waitMsg:'正在提交信息....',
				success:function(response,options){
					var obj = Ext.util.JSON.decode(response.responseText);  	//JSON解码服务器返回的数据
					if(obj.success == true){
						
					}else{
						Ext.Msg.alert('提示',obj.msg);
						store.reload();
						selStore.reload();
					}
				},
				failure:function(response,options){
					Ext.Msg.alert('提示','更新失败!');
					store.reload();
					selStore.reload();
				}
			});
		}
	});
	
	//====================================================================================//
	//新建一个FORM用来接收用户的输入
	addWinForm = new Ext.form.FormPanel({
		width:400,
		height:300,
		plain: true,
		layout :'form',
		labelWidth: 75,
		baseCls:"x-plain",
		labelAlign:'center',
		bodyStyle: "padding:10px 0 0 10px",  
		items:[{
			fieldLabel:'值',
			xtype:'numberfield',
			name:'dict.dictValue',
			id:'dict_Value',
			minValue:0,
			width:160,
			allowBlank:false,
			emptyText:'请输入"数字"...'
		},{
			fieldLabel:'字典名称',
			xtype:'textfield',
			name:'dict.dictName',
			id:'dict_Name',
			minValue:0,
			width:160,
			allowBlank:false,
			emptyText:'请输入"字典名称"...'
		},{
			fieldLabel:'标志',
			xtype:'textfield',
			name:'dict.dictFlag',
			id:'dict_Flag',
			minValue:0,
			width:160,
			allowBlank:false,
			emptyText:'请输入"标志"...'
		},{
			fieldLabel:'备注',
			xtype:'textarea',
			name:'dict.chrnote',
			id:'dict_chrnote',
			minValue:0,
			width:160,
			height:100,
			allowBlank:false,
			emptyText:'请输入"备注"...'
		}]		
	});
	
	//新建一个窗口用来放Form组件的
	addDictWin = new Ext.Window({
		title:'新增Dict',
		width:400,
		height:300,
		plain:true,				//渲染window body的背景为透明背景或浅色背景
	    resizable: true,		//不可以随意改变大小  
		collapsible: true, 		//不允许滚动条
		closeAction:'hide',		//当关闭按钮被点击时执行的动作。“close"或'hide'为可选值
        modal: 'true', 			//弹出模态窗体  
		buttonAlign: "center", 
        bodyStyle: "padding:10px 0 0 30px",  
		items: [addWinForm],  	//在WIN里添加一个FORM
        listeners:{
	        "show":function()
	        {
	            //当window show事件发生时清空一下表单
	            addWinForm.getForm().reset();
	        }
        },
		buttons:[{
			text:'保存',
			minWidth:70,
			handler:function(){
				if(addWinForm.getForm().isValid()){		//如果已经通过验证就执行下一步
					addWinForm.form.submit({
						url:'dict_addDict',
						method:'POST',
						waitTitle:'请稍等....',
						waitMsg:'正在提交信息....',
						baseParams :{'dict.dictId':''},
						success:function(form,action){
							Ext.Msg.alert('提示',action.result.msg);
							addDictWin.hide();
							store.reload();
							selStore.reload();
						},
						failure:function(form,action){
							Ext.Msg.alert('提示',action.result.msg);
						}
					});
				}
			}
		},{
			text:'关闭',
			minWidth:70,
			handler:function(){
				addDictWin.hide();
			}
		}]	
	});
});























